// src/store/modules/config.js
import { defineStore } from 'pinia'
import { getConfigList } from '@/api/modules/config'

export const useConfigStore = defineStore('config', {
    state: () => ({
        // 将配置项存储为 key-value 形式，方便快速查找
        configs: {},
        isLoaded: false,
    }),
    getters: {
        getConfigValue: (state) => (key, defaultValue) => {
            return state.configs[key] || defaultValue;
        },
    },
    actions: {
        async fetchConfigs() {
            try {
                const configList = await getConfigList();
                // 转换数组为 { key: value } 的 map
                this.configs = configList.reduce((acc, cur) => {
                    acc[cur.configKey] = cur.configValue;
                    return acc;
                }, {});
                this.isLoaded = true;
            } catch (error) {
                console.error("获取系统配置失败", error);
                this.isLoaded = false;
            }
        },
    },
})
